// Package main go 实现的压测工具
package main

import (
	"flag"
	"fmt"
	"simqtt-stress/model"
	"simqtt-stress/server"
)

var (
	concurrency uint64 = 200                    // 并发数
	totalNumber uint64 = 1000                   // 请求数(单个并发/协程)
	requestAddr        = "tcp://127.0.0.1:1883" // 压测的mqtt 地址 目前支持，mqtt v5 tcp

	execType uint64 = uint64(model.Pub) // 执行类型
	pubTopic        = "test"
	pubQos   uint64 = 0
	payload         = "test payload"
	subTopic        = "test"
	subQos   uint64 = 0
)

func init() {
	flag.Uint64Var(&concurrency, "c", concurrency, "并发数")
	flag.Uint64Var(&totalNumber, "n", totalNumber, "请求数(单个并发/协程)")
	flag.StringVar(&requestAddr, "a", requestAddr, "压测地址")
	flag.Uint64Var(&execType, "ty", execType, "执行类型，可选1-发布，2-订阅")
	flag.StringVar(&pubTopic, "pt", pubTopic, "发布的主题")
	flag.Uint64Var(&pubQos, "pq", pubQos, "发布的qos")
	flag.StringVar(&payload, "pl", payload, "发布的payload")
	flag.StringVar(&subTopic, "st", subTopic, "订阅的主题")
	flag.Uint64Var(&subQos, "sq", subQos, "订阅的qos")
	// 解析参数
	flag.Parse()
}

// main go 实现的压测工具
// 编译可执行文件
//go:generate go build main.go
func main() {
	//runtime.GOMAXPROCS(1)
	if concurrency == 0 || totalNumber == 0 || (requestAddr == "") {
		fmt.Printf("示例: go run main.go -c 100 -n 1000 -a tcp://127.0.0.1:1883 \n")
		fmt.Printf("压测MQTT地址路径必填 \n")
		fmt.Printf("当前请求参数: -c %d -n %d -u %s \n", concurrency, totalNumber, requestAddr)
		flag.Usage()
		return
	}
	request := model.NewClient("simq-stress", requestAddr, int(totalNumber), 10,
		model.Type(execType), pubTopic, pubQos, payload, subTopic, subQos)

	fmt.Printf("\n 开始启动  并发数:%d 请求数:%d 请求参数: \n", concurrency, totalNumber)
	// 开始处理
	server.Dispose(concurrency, totalNumber, request)
	return
}
